<template>
  <panel :title="title">
    <el-button
      slot="btn"
      type="primary"
      size="mini"
      v-if="!(role === 'normal')"
      @click="dialogVisible = true"
      >添加分类</el-button
    >

    <!-- 查询信息输入框 -->
    <el-form ref="queryForm" :model="queryForm" label-width="80px">
      <el-form-item label="分类">
        <el-input v-model="queryForm.category" placeholder="分类"></el-input>
      </el-form-item>

      <el-form-item class="query-btn">
        <el-button type="warning" @click="queryCate">查询</el-button>
        <el-button type="danger" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 商品分类列表 -->
    <el-table
      ref="categoryTable"
      :data="categoryData"
      highlight-current-row
    >
      <!-- 序号 -->
      <el-table-column type="index" label="序号" width="200"> </el-table-column>
      <!-- 分类名称 -->
      <el-table-column label="分类">
        <template slot-scope="scope">
          <span v-if="!scope.row.isEdit">{{ scope.row.cateName }}</span>
          <el-input v-else v-model="scope.row.cateName" size="mini"></el-input>
        </template>
      </el-table-column>
      <!-- 是否启用 -->
      <el-table-column label="是否启用">
        <template slot-scope="scope">
          <el-switch
            :disabled="!scope.row.isEdit"
            v-model="scope.row.state"
            active-color="#13ce66"
            inactive-color="#cccccc"
          >
          </el-switch>
        </template>
      </el-table-column>
      <!-- 操作 -->
      <el-table-column label="操作" v-if="!(role === 'normal')">
        <template slot-scope="scope">
          <el-button
            :type="scope.row.isEdit ? 'success' : 'default'"
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
            >{{ scope.row.isEdit ? "完成" : "编辑" }}</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        background
        layout="total, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <!-- 编辑弹出模态框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
    >
      <!-- 表单 -->
      <el-form ref="addCategory" :model="addCategory" label-width="90px">
        <el-form-item label="分类名称">
          <el-input v-model="addCategory.cateName" class="account"></el-input>
        </el-form-item>

        <el-form-item label="是否启用">
          <el-switch
            v-model="addCategory.state"
            active-color="#13ce66"
            inactive-color="#cccccc"
          >
          </el-switch>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="addCategoryData"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </panel>
</template>

<script>
import Panel from "@/components/panel/Panel.vue";

import local from '@/utils/local'

// 引入工具函数
import {
  getCategoryList,
  editCategory,
  addCate,
  deleteCate
} from "@/api/goods";

export default {
  components: {
    Panel,
  },
  data() {
    return {
      title: "商品分类",
      queryForm: {
        category: "",
      },
      categoryData: [
        // { id: 1, cateName: "热销榜", state: false },
        // { id: 2, cateName: "热销榜", state: true },
        // { id: 3, cateName: "热销榜", state: true },
        // { id: 4, cateName: "热销榜", state: true },
        // { id: 5, cateName: "热销榜", state: false },
      ],
      value: true,
      dialogVisible: false,
      currentPage: 1, //当前页数
      pageSize: 5,
      total: 14,
      addCategory: {
        cateName: "",
        state: true,
      },
      role: ''
    };
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getCategoryData();
    },
    // 删除分类
    async handleDelete(index,row) {
      try{
        let res = await deleteCate(row.id);
        if (res.data.code === 0) {
          this.getCategoryData()
        }
      }catch(err) {
        console.log(err)
      }

    },
    // 添加分类
    async addCategoryData() {
      this.dialogVisible = false;
      try {
        let res = await addCate(this.addCategory);
        console.log(res);
        if (res.data.code === 0) {
          this.currentPage = Math.ceil(this.total / this.pageSize)
          this.getCategoryData();
        }
      } catch (err) {
        console.log(err);
      }
    },
    handleClose(done) {
      this.$confirm("确认取消编辑？")
        .then((_) => {
          done(); //调用关闭函数  关闭所有窗口
        })
        .catch((_) => {});
    },
    async handleEdit(index, row) {
      row.isEdit = !row.isEdit;

      if (!row.isEdit) {
        // 修改商品分类
        try {
          let res = await editCategory({
            id: row.id,
            cateName: row.cateName,
            state: row.state,
          });

          if (res.data.code === 0) {
           this.getCategoryData();
          }
        } catch (err) {
          console.log(err);
        }
      }
    },
    // 获取商品分类数据
    getCategoryData() {
      getCategoryList({
        currentPage: this.currentPage,
        pageSize: this.pageSize,
      })
        .then((res) => {
          let { total, data } = res.data;

          this.total = total;
          this.categoryData = data;

          // 处理数据
          data.forEach((item) => {
            item.state = item.state === 1 ? true : false;
            item.isEdit = false;
          });
        })
        .catch((err) => {
          console.log(err);
        });
    },
    async queryCate() {
      this.currentPage = 1;

      this.categoryData = this.categoryData.filter((item) => {
        return item.cateName.includes(this.queryForm.category);
      });
    },
    reset() {
      this.queryForm = {};
      this.getCategoryData();
    },
   
  },
  created() {
    this.getCategoryData();

    this.role = local.get('role')

  },
};
</script>

<style lang="less" scoped>
.pagination {
  margin-top: 20px;
}

/deep/ .el-form {
  display: flex;
  flex-wrap: wrap;
  /deep/ .el-input {
    width: 200px;
  }
}

.query-btn {
  /deep/ .el-form-item__content {
    margin-left: 20px !important;
  }
}
</style>